package com.test.latitudex;

import com.test.latitudex.utilities.RegistrationManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class CreateAccountActivity extends Activity {

	@SuppressWarnings("unused")
	private static final String LOG_TAG = "CreateAccount";

	private static Button registerButton;
	private static Button finishButton;
	private static EditText email;
	private static EditText pin;
	private static EditText displayName;
	private static EditText password;
	private static EditText passConfirm;
	private static RegistrationManager rManager;
	private static RelativeLayout partOne;
	private static RelativeLayout partTwo;
	private static Context ctx;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_account_layout);
		ctx = this;
		rManager = RegistrationManager.getEntity();
		registerButton = (Button) findViewById(R.id.registerButton);
		finishButton = (Button) findViewById(R.id.finishButton);
		email = (EditText) findViewById(R.id.emailEdit);
		pin = (EditText) findViewById(R.id.pinCodeEdit);
		displayName = (EditText) findViewById(R.id.displayNameEdit);
		password = (EditText) findViewById(R.id.passEdit);
		passConfirm = (EditText) findViewById(R.id.confirmPassEdit);
		partOne = (RelativeLayout) findViewById(R.id.firstStepPart);
		partTwo = (RelativeLayout) findViewById(R.id.secondStepPart);

		partTwo.setVisibility(View.GONE);

		registerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				new Thread(new Runnable() {
					public void run() {
						try {
							if (rManager.registerId(displayName.getText().toString(), email.getText().toString(), "1")) {
								partOne.setVisibility(View.GONE);
								partTwo.setVisibility(View.VISIBLE);
							}
						} catch (Exception e) {
							Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT).show();
						}
					}
				}).run();
			}
		});

		finishButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					public void run() {
						if (password.getText().toString().equals(passConfirm.getText().toString())) {
							try {
								rManager.registerUser(pin.getText().toString(), password.getText().toString());
								finish();
							} catch (Exception e) {
								Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT).show();
							}
						} else {
							Toast.makeText(ctx, "Given passwords are different. Try again!", Toast.LENGTH_SHORT).show();
							;
						}

					}

				}).run();
			}
		});
	}
}
